//
//  TeamHelper.swift
//  NETeamUIKit
//
//  Created by Laura on 2024/8/6.
//

import Foundation
import NIMSDK
import NECommonKit

@objcMembers
open class AnnouncementUserModel: NSObject {
  public var id: String?
  public var time: String?
  public var member: NETeamMemberInfoModel?

  override public init() {}
}


@objcMembers
open class TeamHelper: NSObject {
    /// 用户角色
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func userTitle(_ member: NETeamMemberInfoModel?) -> Int {
        if let member = member, let serverExtension = member.nimUser?.user?.serverExtension {
            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let title = jsonObj["title"] as? Int {
                return title
            }
        }
        return 0
    }


    /// 用户头衔
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func userTitleName(_ member: NETeamMemberInfoModel?) -> String {
        if let member = member, let serverExtension = member.nimUser?.user?.serverExtension {
            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let titleName = jsonObj["titleName"] as? String {
                return titleName
            }
        }
        return ""
    }
    
    /// 公告发布人
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func announcemenUserInfo(_ teamId: String?, _ serverExtension: String?, _ completion: @escaping (AnnouncementUserModel?) -> Void) {
        if let serverExtension = serverExtension {
            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let announcement_user = jsonObj[keyAnnouncementUser] as? [String: Any] {
                
                let user = AnnouncementUserModel()
                user.id = announcement_user["id"] as? String
                user.time = announcement_user["time"] as? String
                
                if let userId = user.id {
                    ContactRepo.shared.getUserWithFriend(accountIds: [userId]) { users, v2Error in
                      if v2Error == nil {
                          let model = NETeamMemberInfoModel()
                          model.nimUser = users?.first
                          user.member = model
                          completion(user)
                      }
                    }
                }
            }
        }
    }
    
    ///  备注
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func teamAlias(_ acid: String, _ serverExtension: String?) -> String {
        if let serverExtension = serverExtension {
            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let alias = jsonObj[keyAlias] as? String {
                return alias
            }
//            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let alias = jsonObj["alias_\(acid)"] as? String {
//                return alias
//            }
        }
        return ""
    }
    
    
    /// 群聊是否可以备注
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func canAlias(_ member: NETeamMemberInfoModel?) -> Bool {
        if let member = member, let serverExtension = member.nimUser?.user?.serverExtension {
            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let title = jsonObj["title"] as? Int {
                return title > 0
            }
        }
        return false
    }

    /// 群聊是否可以删除
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func canDeleteMemeber(_ member: NETeamMemberInfoModel?) -> Bool {
        if let member = member, let serverExtension = member.nimUser?.user?.serverExtension {
            if member.teamMember?.memberRole == .TEAM_MEMBER_ROLE_MANAGER || member.teamMember?.memberRole == .TEAM_MEMBER_ROLE_OWNER {
                if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let title = jsonObj["title"] as? Int {
                    return title > 0
                }
            }
        }
        return false
    }
    
    /// 群聊是否可以编辑群公告
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func canEditAnnouncement(_ member: NETeamMemberInfoModel?) -> Bool {
        if let member = member, let serverExtension = member.nimUser?.user?.serverExtension {
            if member.teamMember?.memberRole == .TEAM_MEMBER_ROLE_MANAGER || member.teamMember?.memberRole == .TEAM_MEMBER_ROLE_OWNER {
                if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let title = jsonObj["title"] as? Int {
                    return title > 0
                }
            }
        }
        return false
    }
    
    /// 群聊是否可以编辑群信息
    /// - Parameter member : 群成员信息
    /// - Parameter completion: 完成回调
    public static func canEditInfo(_ member: NETeamMemberInfoModel?) -> Bool {
        if let member = member, let serverExtension = member.nimUser?.user?.serverExtension {
            if let jsonObj = NECommonUtil.getDictionaryFromJSONString(serverExtension), let title = jsonObj["title"] as? Int {
                return title > 0
            }
        }
        return false
    }
    
    
}
